package com.lgqz.topsports.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.lgqz.topsports.R;
import com.lgqz.topsports.adapter.CommentRecyclerAdapter;
import com.lgqz.topsports.bean.DataEntity;
import com.lgqz.topsports.bean.NewEntity;
import com.lgqz.topsports.databinding.FragmentBaseBinding;
import com.lgqz.topsports.util.HttpService;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseFragment extends Fragment implements Callback<NewEntity>, OnRefreshListener, OnLoadMoreListener {


    private int mId;
    private FragmentBaseBinding binding;
    private CommentRecyclerAdapter<Object> adapter;
    private SwipeToLoadLayout swipeToLoadLayout;
    private DataEntity data;

    public BaseFragment() {
        // Required empty public constructor
    }

    public static BaseFragment newInstance(String title) {

        Bundle args = new Bundle();
        args.putString("title", title);
        BaseFragment fragment = new BaseFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        String title = bundle.getString("title");
        mId = getUrlId(title);
    }

    private int getUrlId(String title) {

        int id = 0;
        switch (title) {
            case "推荐":
                id = 0;
                break;
            case "国际足球":
                id = 1;
                break;
            case "NBA":
                id = 2;
                break;
            case "网球":
                id = 3;
                break;
            case "羽毛球":
                id = 5;
                break;
            case "高尔夫":
                id = 6;
                break;
            case "中国足球":
                id = 8;
                break;
            case "中国篮球":
                id = 9;
                break;
            case "图说":
                id = 11;
                break;
            case "视频":
                id = 12;
                break;
            case "竞猜":
                id = 13;
                break;
            case "电竞":
                id = 17;
                break;
            case "最董球":
                id = 19;
                break;
            case "铿锵玫瑰":
                id = 20;
                break;
        }
        return id;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_base, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        swipeToLoadLayout = binding.swipeToLoadLayout;
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
        adapter = new CommentRecyclerAdapter<>(new ArrayList<>(), getContext());
        binding.swipeTarget.setAdapter(adapter);
        autoRefresh();
        initData();

    }

    private void initData() {
        HttpService service = new Retrofit.Builder()
                .baseUrl("http://api.lifxapi.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getClasfication(mId, "216.9", true, "865682026799303", "key%3ATiyuForAndroid", "2.11",
                "json", false).enqueue(this);
    }

    @Override
    public void onResponse(Call<NewEntity> call, Response<NewEntity> response) {
        data = response.body().getData();
        if ((response.body().getData().getBanners() != null) && (response.body().getData().getBanners().size() != 0)) {
            adapter.add(data);
        }
        if (response.body().getData().getTops() != null) {
            adapter.addAll(response.body().getData().getTops());
        }
        if (response.body().getData().getContents() != null) {
            adapter.addAll(response.body().getData().getContents());
        }
    }

    @Override
    public void onFailure(Call<NewEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRefresh() {
        swipeToLoadLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeToLoadLayout.setRefreshing(false);
                adapter.removeAll();
                initData();
                // mAdapter.add("REFRESH:\n" + new Date());
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        swipeToLoadLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeToLoadLayout.setLoadingMore(false);
                getLoadMore();
                // mAdapter.add("LOAD MORE:\n" + new Date());
            }
        }, 2000);

    }

    private void getLoadMore() {
        HttpService service = new Retrofit.Builder()
                .baseUrl("http://api.lifxapi.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getLoadMoreData(mId, data.getContents().get(data.getContents().size()-1).getId(), "216.9", true, "865682026799303", "key%3ATiyuForAndroid", "2.11",
                "json", false).enqueue(this);
    }

    private void autoRefresh() {
        swipeToLoadLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeToLoadLayout.setRefreshing(true);
            }
        });
    }
}
